#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include "stm32mp157_rcc.h"
#include "stm32mp157_gpio.h"
#include "led_plat.h"

#define RCC_START       RCC
#define RCC_END         (RCC + sizeof(rcc_t) - 1)
#define GPIOZ_START     GPIOZ
#define GPIOZ_END       (GPIOZ + sizeof(gpio_t) - 1)
#define GPIOE_START     GPIOE
#define GPIOE_END       (GPIOE + sizeof(gpio_t) - 1)
#define GPIOF_START     GPIOF
#define GPIOF_END       (GPIOF + sizeof(gpio_t) - 1)

struct led_plat_data led_data = {
	//哪个灯
	.led_name = "stm32mp157_led",
	//设备节点
	.dev_name = "led_drv",
	//次设备号
	.minor = 0,

	//RCC
	.RCC_PLL4CR_SET = (1 << 0),
	.RCC_PLL4CR_TEST = (1 << 1),
	.RCC_MP_AHB4ENSETR_SET = (1 << 4) | (1 << 5),
	//GPIOZ
	.GPIOZ_MODER_RESET = ~((0x03 << 10) | (0x03 << 12) | (0x03 << 14)),
	.GPIOZ_MODER_SET = (0x01 << 10) | (0x01 << 12) | (0x01 << 14),
	.GPIOZ_OTYPER_RESET = ~((0x01 << 5) | (0x01 << 6) | (0x01 << 7)),
	.GPIOZ_OSPEEDR_RESET = ~((0x03 << 10) | (0x03 << 12) | (0x03 << 14)),
	.GPIOZ_ODR_RESET = ~((0x01 << 5) | (0x01 << 6) | (0x01 << 7)),
	.GPIOZ_ODR_SET = (0x01 << 5) | (0x01 << 6) | (0x01 << 7),

	//GPIOE
	.GPIOE_MODER_RESET = ~((0x03 << 16) | (0x03 << 20)),
	.GPIOE_MODER_SET = (0x01 << 16) | (0x01 << 20),
	.GPIOE_OTYPER_RESET = ~((0x01 << 8) | (0x01 << 10)),
	.GPIOE_OSPEEDR_RESET = ~((0x03 << 16) | (0x03 << 20)),
	.GPIOE_ODR_RESET = ~((0x01 << 8) | (0x01 << 10)),
	.GPIOE_ODR_SET = (0x01 << 8) | (0x01 << 10),

	//GPIOF
	.GPIOF_MODER_RESET = ~(0x03 << 20),
	.GPIOF_MODER_SET = (0x01 << 20),
	.GPIOF_OTYPER_RESET = ~(0x01 << 10),
	.GPIOF_OSPEEDR_RESET = ~(0x03 << 20),
	.GPIOF_ODR_RESET = ~(0x01 << 10),
	.GPIOF_ODR_SET = (0x01 << 10),
};


struct resource	led_res[] = {
	[0] = {
		.start = RCC_START,
		.end = RCC_END,
		.name = "rcc",
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = GPIOZ_START,
		.end = GPIOZ_END,
		.name = "gpioz",
		.flags = IORESOURCE_MEM,
	},
	[2] = {
		.start = GPIOE_START,
		.end = GPIOE_END,
		.name = "gpioe",
		.flags = IORESOURCE_MEM,
	},
	[3] = {
		.start = GPIOF_START,
		.end = GPIOF_END,
		.name = "gpiof",
		.flags = IORESOURCE_MEM,
	},
};

void led_dev_release(struct device *dev)
{
}

struct platform_device led_dev = {
	.name = "stm32mp157_led", //名称
	.id = 0, //ID用来区分每个设备
	.dev = {
		.release = led_dev_release,
		.platform_data = &led_data, 
	},
	.num_resources = ARRAY_SIZE(led_res),//资源个数
	.resource = led_res,//资源的起始地址
};

static int __init led_dev_init(void)
{
	return platform_device_register(&led_dev);
}

static void __exit led_dev_exit(void)
{
	platform_device_unregister(&led_dev);
}

module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");

